extends Node


@onready var select_char_areas: Node2D = $SelectCharAreas
@onready var select_char_circle: Sprite2D = %SelectChar

#region start menu
@onready var start_btn: Button = %StartBtn
@onready var settings_btn: Button = %SettingsBtn
@onready var exit_btn: Button = %ExitBtn
#endregion

#region player info panel
@onready var player_info_panel: Panel = %PlayerInfoPanel
@onready var char_name_lbl: Label = %CharNameLbl
@onready var char_info_lbl: RichTextLabel = %CharInfoLbl
@onready var back_btn: Button = %BackBtn
@onready var confirm_player_btn: Button = %ConfirmPlayerBtn
#endregion

@onready var camera: Camera2D = %Camera2D
@onready var amt_player: AnimationPlayer = %AnimationPlayer


@export var _default_player: Player

var _selected_char: Player:
    set(value):
        if _selected_char == value: return
        
        _selected_char = value
        
        if  _selected_char == null: 
            select_char_circle.hide()
            return
        
        select_char_circle.show()
        _update_select_char_circle_pos(_selected_char)
        _update_player_info_panel(_selected_char)

enum StartSceneState {
    START_MENU,
    SELECTING_CHARACTER,
    SELECTED_CHARACTER,
}

@export var _state: StartSceneState:
    set(value):
        _state = value
        if _state == StartSceneState.START_MENU:
            # 未进入选择角色状态时，禁止SelectCharacterArea
            select_char_areas.process_mode = Node.PROCESS_MODE_DISABLED
            select_char_circle.hide()
        elif _state == StartSceneState.SELECTING_CHARACTER:
            select_char_areas.process_mode = Node.PROCESS_MODE_INHERIT
            select_char_circle.show()
            # 设置默认角色
            if _selected_char == null:
                _selected_char = _default_player
        elif _state == StartSceneState.SELECTED_CHARACTER:
            select_char_areas.process_mode = Node.PROCESS_MODE_DISABLED
            player_info_panel.hide()
            select_char_circle.hide()
            

const _CHAR_INFO_FORMAT: String = \
"生命值：{health}
护甲值：{shield}
攻击力：{attack}
防御力：{defense}
移速：{speed}
攻速：{attack_speed}
暴击率：{critical_rate}
暴击伤害：{critical_degree}
闪避率：{dodge_rate}
冲刺数：{dash_count}
[color=#{fire_color}]火属性伤害加成：{fire_damage_add}[/color]
[color=#{ice_color}]冰属性伤害加成：{ice_damage_add}[/color]
[color=#{thunder_color}]雷属性伤害加成：{thunder_damage_add}[/color]

技能：{skill_description}"

## camera:
## start: pos(414, 186), scale(1, 1)
## select: pos(510, 186), scale(1.5, 1.5)
## end: pos(follow player), scale(1, 1)

func _ready() -> void:
    #region start menu
    start_btn.pressed.connect(amt_player.play.bind("start_camera"))

    settings_btn.pressed.connect(
        func() -> void:
            pass
    )

    exit_btn.pressed.connect(get_tree().quit)
    #endregion
    
    #region player info panel
    back_btn.pressed.connect(amt_player.play_backwards.bind("start_camera"))
    
    confirm_player_btn.pressed.connect(
        func() -> void:
            Global.init_player(_selected_char)
            _state = StartSceneState.SELECTED_CHARACTER
            
            var tween := create_tween().bind_node(self)
            tween.tween_property(camera, "zoom", Vector2(1, 1), 0.5)
            tween.set_parallel(true)
            tween.tween_property(camera, "global_position", _selected_char.center_position(), 0.5)
            tween.finished.connect(
                func() -> void:
                    _selected_char.bind_camera(camera)
                    _selected_char.can_control = true
            )
            tween.play()
    )
    
    #endregion
    
    for child in get_node("YSort/Characters").get_children():
        if child is not Character: continue
        child.can_control = false
    
    for area: SelectCharacterArea in select_char_areas.get_children():
        area.selected_character.connect(_on_select_char_area_selected_character)

    camera.global_position = Vector2(414, 186)
    camera.zoom = Vector2(1, 1)
    
    _state = StartSceneState.START_MENU
    

func _update_select_char_circle_pos(character: Character) -> void:
    var tween := create_tween().bind_node(self)
    tween.set_trans(Tween.TRANS_SPRING)
    tween.tween_property(select_char_circle, "global_position", character.global_position - Vector2(0, 2), 0.25)
    tween.play()


func _update_player_info_panel(character: Character) -> void:
    char_name_lbl.text = character.data.name_str
    var info_dict := {
        "health": character.data.max_health.int_value(),
        "shield": character.data.max_shield.int_value(),
        "attack": character.data.attack.int_value(),
        "defense": character.data.defense.int_value(),
        "speed": character.data.speed.int_value(),
        "attack_speed": character.data.attack_speed.value * 100,
        "critical_rate": "%d%%" % [int(character.data.critical_rate.value * 100)],
        "critical_degree": "%d%%" % [int(character.data.critical_degree.value * 100)],
        "dodge_rate": "%d%%" % [int(character.data.dodge_rate.value * 100)],
        "dash_count": character.data.dash_count.int_value(),
        "fire_damage_add": 0,
        "ice_damage_add": 0,
        "thunder_damage_add": 0,
        "skill_description": "无",
        "fire_color": DamageInfo.get_damage_attribute_clr(DamageInfo.DamageAttribute.FIRE).to_html(),
        "ice_color": DamageInfo.get_damage_attribute_clr(DamageInfo.DamageAttribute.ICE).to_html(),
        "thunder_color": DamageInfo.get_damage_attribute_clr(DamageInfo.DamageAttribute.THUNDER).to_html(),
    }
    
    char_info_lbl.text = _CHAR_INFO_FORMAT.format(info_dict)


func _on_select_char_area_selected_character(character: Character) -> void:
    _selected_char = character
